package com.jiang.myfm.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.jiang.myfm.R;
import com.jiang.myfm.SearchActivity;
import com.jiang.myfm.interfaces.SetFirstListen;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/2.
 */
public class Findfragment extends Fragment {
    private Button btn;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private List<Fragment> list;
    private String[] titles = new String[]{"推荐", "分类", "广播", "榜单", "主播"};
    private SetFirstListen mSetFirstListen;

    public SetFirstListen getSetFirstListen() {
        return mSetFirstListen;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.findfragment_item, container, false);
        btn = (Button) view.findViewById(R.id.toolbar_btn);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout);
        mSetFirstListen=new SetFirstListen() {
            @Override
            public void onSetFirstListen(int position) {
                tabLayout.getTabAt(position).select();
            }
        };
        viewPager = (ViewPager) view.findViewById(R.id.vp);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDate();
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

            @Override
            public int getCount() {
                return list.size();
            }
        };
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initDate() {
        list = new ArrayList<>();
        Findfragment_1 fragment1 = new Findfragment_1();
        Findfragment_2 fragment2 = new Findfragment_2();
        Findfragment_3 fragment3 = new Findfragment_3();
        Findfragment_4 fragment4 = new Findfragment_4();
        Findfragment_5 fragment5 = new Findfragment_5();
        list.add(fragment1);
        list.add(fragment2);
        list.add(fragment3);
        list.add(fragment4);
        list.add(fragment5);

    }
}
